﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Physis.Security {

    [DataContract]
    public class Session {

        #region Private Properties 

        [DataMember]
        private Guid token = Guid.NewGuid ();

        [DataMember]
        private Int64 securityAuthorityId = 0;

        [DataMember]
        private String securityAuthorityName = String.Empty;

        [DataMember]
        private Int64 userAccountId = 0;

        [DataMember]
        private String userAccountName = String.Empty;

        [DataMember]
        private String userDisplayName = String.Empty;


        [DataMember]
        private Int64 environmentId = 0;

        [DataMember]
        private String environmentName = String.Empty;


        [DataMember]
        private List<String> enterprisePermissions = new List<String> ();

        [DataMember]
        private List<String> environmentPermissions = new List<String> ();
        

        [DataMember]
        private DateTime lastActivityDateTime = DateTime.Now;

        #endregion 


        #region Public Properties 
        
        public Guid Token { get { return token; } }


        public Int64 SecurityAuthorityId { get { return securityAuthorityId; } }

        public String SecurityAuthorityName { get { return securityAuthorityName; } }


        public Int64 UserAccountId { get { return userAccountId; } }

        public String UserAccountName { get { return userAccountName; } }

        public String UserDisplayName { get { return ((String.IsNullOrWhiteSpace (userDisplayName)) ? userAccountName : userDisplayName); } }


        public Int64 EnvironmentId { get { return environmentId; } }

        public String EnvironmentName { get { return environmentName; } }

        
        public List<String> EnterprisePermissions { get { return enterprisePermissions; } }

        public List<String> EnvironmentPermissions { get { return environmentPermissions; } }


        public DateTime LastActivityDateTime { get { return lastActivityDateTime; } set { lastActivityDateTime = value; } }

        #endregion 


        #region Constructors 
        
        public Session (Application forApplication, SecurityAuthority forSecurityAuthority, UserAccount forUserAccount, Enterprise.Environment forEnvironment) {

            securityAuthorityId = forSecurityAuthority.Id;

            securityAuthorityName = forSecurityAuthority.Name;


            userAccountId = forUserAccount.Id;

            userAccountName = forUserAccount.Name;

            userDisplayName = forUserAccount.DisplayName;


            if (forEnvironment != null) {

                environmentId = forEnvironment.Id;

                environmentName = forEnvironment.Name;

            }


            enterprisePermissions = forUserAccount.EnterprisePermissions;


            SerializeToDatabase (forApplication);

            return;

        }

        public Session (Application forApplication, SecurityAuthority forSecurityAuthority, UserAccount forUserAccount) : this (forApplication, forSecurityAuthority, forUserAccount, null) {

            return;

        }

        public Session (Application forApplication, Guid forToken) {

            token = forToken;

            DeserializeFromDatabase (forApplication);

            SerializeToDatabase (forApplication);

            return;

        }

        #endregion 


        #region Database Functions 

        private void SerializeToDatabase (Application application) {

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream ();

            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer (this.GetType());

            serializer.WriteObject (memoryStream, this); 

            memoryStream.Seek (0, System.IO.SeekOrigin.Begin);

            String serializedData = new System.IO.StreamReader (memoryStream).ReadToEnd ();
            
            return;

        }

        private void DeserializeFromDatabase (Application application) {


        }

        #endregion 


        #region Public Methods

        public Boolean HasEnterprisePermission (String permissionName) {

            return enterprisePermissions.Contains (permissionName);

        }

        #endregion 

    }

}
